home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / text / hyper / ADtoHT2_1.lha / Source.lha / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-23  |  9.1 KB  |  372 lines

  1. #ifndef EXEC_EXECBASE_H
  2. #include <exec/execbase.h>
  3. #endif
  4.  
  5. #ifndef DOS_DOSEXTENS_H
  6. #include <dos/dosextens.h>
  7. #endif
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16.  
  17. /************************************************************************/
  18.  
  19. #include "main.h"
  20. #include "File.h"
  21. #include "Includes.h"
  22. #include "ProcessDir.h"
  23. #include "Autodocs.h"
  24. #include "AdditionalDocs.h"
  25. #include "Version.h"
  26.  
  27. /************************************************************************/
  28.  
  29. struct Arguments Arguments;
  30.  
  31. int Pass2;
  32.  
  33. BPTR Dirs[DIRCOUNT + 1];
  34.  
  35. #ifdef DEBUG
  36. FILE *DebugFile;
  37. #endif
  38.  
  39. /************************************************************************/
  40.  
  41. static int RC;
  42. static struct RDArgs *RDArgs;
  43.  
  44. /************************************************************************/
  45.  
  46. /* not static to avoid problems */
  47. char VersionString[] = "$VER: " PROGRAM_NAME " " PROGRAM_VERSION " (" PROGRAM_DATE ")" PROGRAM_COPYRIGHT "; compiled for " CPU " by " COMPILER;
  48.  
  49. /************************************************************************/
  50. /*                                                                      */
  51. /* Compare two AnyNodes                                                 */
  52. /*                                                                      */
  53. /************************************************************************/
  54.  
  55. int
  56. nodecmp (struct AVLNode *Node1, struct AVLNode *Node2)
  57.  
  58. {
  59.   int Result;
  60.  
  61.   Result = strcasecmp (((struct AnyNode *)Node1)->Name, ((struct AnyNode *)Node2)->Name);
  62.   if (Result == 0)
  63.     {
  64.       Result = strcmp (((struct AnyNode *)Node1)->Name, ((struct AnyNode *)Node2)->Name);
  65.     }
  66.   return Result;
  67. }
  68.  
  69. int
  70. nodecasecmp (struct AVLNode *Node1, struct AVLNode *Node2)
  71.  
  72. {
  73.   return strcasecmp (((struct AnyNode *)Node1)->Name, ((struct AnyNode *)Node2)->Name);
  74. }
  75.  
  76. #if !defined(__GNUC__) || !defined(__OPTIMIZE__)
  77. /************************************************************************/
  78. /*                                                                      */
  79. /* Search an AnyNode                                                    */
  80. /*                                                                      */
  81. /************************************************************************/
  82.  
  83. struct AnyNode *
  84. SearchNode (struct AVLTree *Tree, char *Name)
  85.  
  86. {
  87.   struct AnyNode AnyNode;
  88.  
  89.   AnyNode.Name = Name;
  90.   return (struct AnyNode *) AVL_SearchNode (Tree, &AnyNode.AVLNode);
  91. }
  92. #endif
  93.  
  94. /************************************************************************/
  95. /*                                                                      */
  96. /* Create an AnyNode.                                                   */
  97. /*                                                                      */
  98. /************************************************************************/
  99.  
  100. struct AnyNode *
  101. CreateNode (const char *Name, size_t Size)
  102.  
  103. {
  104.   struct AnyNode *AnyNode;
  105.  
  106.   AnyNode = xmalloc (Size);
  107.   AnyNode->Name = xstrdup (Name);
  108.   return AnyNode;
  109. }
  110.  
  111. /************************************************************************/
  112. /*                                                                      */
  113. /* Set the return code for the shell                                    */
  114. /*                                                                      */
  115. /************************************************************************/
  116.  
  117. void
  118. SetRC (int NewRC)
  119.  
  120. {
  121.   if (RC < NewRC)
  122.     RC = NewRC;
  123. }
  124.  
  125. /************************************************************************/
  126. /*                                                                      */
  127. /* General cleanup.                                                     */
  128. /*                                                                      */
  129. /************************************************************************/
  130.  
  131. void
  132. CloseAll (int NewRC)
  133.  
  134. {
  135.   SetRC (NewRC);
  136.  
  137.   if (CallMatchEnd)
  138.     MatchEnd (&AnchorPath.AnchorPath);
  139.  
  140.   CurrentDir (Dirs[CURRENTDIR]);
  141.  
  142.   RClose ();
  143.   WClose ();
  144.  
  145.   FreeArgs (RDArgs);
  146.  
  147.   {
  148.     int i;
  149.     for (i = 0; i < DIRCOUNT; i++)
  150.       {
  151.     if (Dirs[i])
  152.       UnLock (Dirs[i]);
  153.       }
  154.   }
  155.  
  156.   exit (RC);
  157. }
  158.  
  159. /************************************************************************/
  160. /*                                                                      */
  161. /* xmalloc(): allocate a block, exit on error                           */
  162. /*                                                                      */
  163. /************************************************************************/
  164.  
  165. void *xmalloc (size_t Size)
  166.  
  167. {
  168.   int FirstError;
  169.  
  170.   FirstError=TRUE;
  171.   do
  172.     {
  173.       size_t *Memory;
  174.  
  175.       if ((Memory = malloc (Size)))
  176.     {
  177.       if (!FirstError)
  178.         {
  179.           fprintf(stderr,"Allocation succeeded.\n");
  180.         }
  181.       return Memory;
  182.     }
  183.       if (FirstError)
  184.     {
  185.       perror (PROGRAM_NAME);
  186.       FirstError=FALSE;
  187.     }
  188.       Delay(2*TICKS_PER_SECOND);
  189.     }
  190.   while (!CheckSignal(SIGBREAKF_CTRL_C));
  191.   errno=ERROR_BREAK;
  192.   perror(NULL);
  193.   CloseAll (RETURN_ERROR);
  194. }
  195.  
  196. /************************************************************************/
  197. /*                                                                      */
  198. /* xrealloc(): re-allocate a block, exit on error.            */
  199. /*                                                                      */
  200. /************************************************************************/
  201.  
  202. void *xrealloc(void *Memory, size_t Size)
  203.  
  204. {
  205.   int FirstError;
  206.  
  207.   FirstError=TRUE;
  208.   do
  209.     {
  210.       size_t *NewMemory;
  211.  
  212.       if ((NewMemory=realloc(Memory,Size)))
  213.     {
  214.       if (!FirstError)
  215.         {
  216.           fprintf(stderr,"Allocation succeeded.\n");
  217.         }
  218.       return NewMemory;
  219.     }
  220.       if (FirstError)
  221.     {
  222.       perror(PROGRAM_NAME);
  223.       FirstError=FALSE;
  224.     }
  225.     }
  226.   while (!CheckSignal(SIGBREAKF_CTRL_C));
  227.   errno=ERROR_BREAK;
  228.   perror(NULL);
  229.   CloseAll(RETURN_ERROR);
  230. }
  231.  
  232. /************************************************************************/
  233. /*                                                                      */
  234. /* Duplicate a string, exit on error                                    */
  235. /*                                                                      */
  236. /************************************************************************/
  237.  
  238. char *xstrdup (const char *String)
  239.  
  240. {
  241.   char *New;
  242.  
  243.   New = xmalloc (strlen (String) + 1);
  244.   strcpy (New, String);
  245.   return New;
  246. }
  247.  
  248. /************************************************************************/
  249. /*                                                                      */
  250. /* Init everything we need to get going.                                */
  251. /*                                                                      */
  252. /************************************************************************/
  253.  
  254. static void
  255. Init (void)
  256.  
  257. {
  258.   CurrentDir (Dirs[CURRENTDIR] = CurrentDir (NULL));
  259. }
  260.  
  261. /************************************************************************/
  262. /*                                                                      */
  263. /* Process the command line                                             */
  264. /*                                                                      */
  265. /************************************************************************/
  266.  
  267. static void
  268. GetParams (void)
  269.  
  270. {
  271.   static long DefaultWidth = 78;
  272.   int i;
  273.  
  274.   Arguments.Width = &DefaultWidth;
  275.  
  276.   if (!(RDArgs = ReadArgs ("INC/A,HINC/A,DOC/A,HDOC/A,"
  277.                "MASTER,"
  278.                "XREF,"
  279.                "FULLPATH/S,"
  280.                "VERSION/N,"
  281.                "PARENTHESES/S,"
  282.                "WIDTH/N"
  283. #ifdef DEBUG
  284.                ",DEBUG"
  285. #endif
  286.                ,(long *) &Arguments, NULL)))
  287.     {
  288.       errno=IoErr();
  289.       perror (PROGRAM_NAME);
  290.       CloseAll (RETURN_FAIL);
  291.     }
  292.  
  293.   if (*Arguments.Width < 40)
  294.     {
  295.       fprintf (stderr, "Warning: WIDTH must be at least 40. Using WIDTH=40.\n");
  296.       DefaultWidth = 40;
  297.       Arguments.Width = &DefaultWidth;
  298.       SetRC (RETURN_WARN);
  299.     }
  300.  
  301.   if (!Arguments.Version)
  302.     {
  303.       static long AmigaguideVersion;
  304.  
  305.       struct Library *Library;
  306.  
  307.       if ((Library = OpenLibrary ("amigaguide.library", 0)) ||
  308.       (Library = OpenLibrary ("version.library", 0)))
  309.     {
  310.       AmigaguideVersion = Library->lib_Version;
  311.       CloseLibrary (Library);
  312.     }
  313.       else
  314.     {
  315.       AmigaguideVersion = SysBase->LibNode.lib_Version;
  316.     }
  317.       Arguments.Version = &AmigaguideVersion;
  318.     }
  319.  
  320.   for (i = 0; i < DIRCOUNT; i++)
  321.     {
  322.       if (!(Dirs[i] = Lock (Arguments.Dirs[i], SHARED_LOCK)))
  323.     {
  324.       errno=IoErr();
  325.       perror (Arguments.Dirs[i]);
  326.       CloseAll (RETURN_FAIL);
  327.     }
  328.     }
  329.  
  330. #ifdef DEBUG
  331.   if (Arguments.Debug)
  332.     {
  333.       if (!(DebugFile=fopen(Arguments.Debug,"w")))
  334.     {
  335.       perror(Arguments.Debug);
  336.       CloseAll(RETURN_FAIL);
  337.     }
  338.       SetVBuf (fileno(DebugFile), NULL, BUF_FULL, 32 * 1024);
  339.     }
  340. #endif
  341. }
  342.  
  343. /************************************************************************/
  344.  
  345. void
  346. AmigaMain (void)
  347.  
  348. {
  349.   if (WorkbenchMessage)
  350.     {
  351.       exit (100);
  352.     }
  353.  
  354.   Init ();
  355.   GetParams ();
  356.  
  357.   puts ("*** First pass ***");
  358.   ProcessDir1 (INCLUDEDIR, "#?.h", ProcessIncludeFile1);
  359.   ProcessDir1 (AUTODOCDIR, "#?.doc", ProcessAutodocFile1);
  360.  
  361.   Pass2 = TRUE;
  362.  
  363.   WriteGlobalTOC ();
  364.   WriteXRef ();
  365.  
  366.   puts ("\n*** Second pass ***");
  367.   ProcessDir2 (INCLUDEDIR, &IncludeFileTree, (void (*)(struct AnyNode *)) ProcessIncludeFile2);
  368.   ProcessDir2 (AUTODOCDIR, &AutodocFileTree, (void (*)(struct AnyNode *)) ProcessAutodocFile2);
  369.  
  370.   CloseAll (RETURN_OK);
  371. }
  372.